package com.bac.rentmap.ad.flat.rating.item;

import java.util.Date;
import java.util.logging.Logger;

import com.bac.rentmap.ad.flat.FlatAd;

public class PublishingIntervalRatingItemCalculator implements
		RatingItemCalculator {
	private static final Logger log = Logger
			.getLogger(PublishingIntervalRatingItemCalculator.class.getName());
	private static final int MAX_POSSIBLE_RATING = 0;
	private static final int SIGNIFICANT_INTERVAL_IN_DAYS = 10;

	/**
	 * This rating is used to avoid bad flat ads to be propagated to the top of
	 * search result by posting them frequently. Significant comparing to last
	 * publishing date rating in case interval is greater or equal to
	 * {@link #SIGNIFICANT_INTERVAL_IN_DAYS} days.
	 * 
	 * @param flatAd
	 *            required
	 * @return less or equal to zero rating
	 */
	public long calculate(FlatAd flatAd) {
		Date lastPublishingDate = flatAd.getLastPublishingDate();
		Date firstPublishingDate = flatAd.getFirstPublishingDate();
		long res;
		if (lastPublishingDate != null && firstPublishingDate != null) {
			res = -(DateRatingHelper.getDateRating(lastPublishingDate) - DateRatingHelper
					.getDateRating(firstPublishingDate))
					/ SIGNIFICANT_INTERVAL_IN_DAYS;
		} else {
			// TODO: remove
			log.warning("First or last publishing date is not set for "
					+ flatAd);
			res = MAX_POSSIBLE_RATING;
		}
		log.finest("Publishing interval rating: " + res);
		return res;
	}

	public long getMaxPossibleRating() {
		return MAX_POSSIBLE_RATING;
	}

	public long getMinPossibleRating() {
		long res = -(DateRatingHelper.getMaxDateRating() - DateRatingHelper
				.getMinDateRating()) / SIGNIFICANT_INTERVAL_IN_DAYS;
		return res;
	}
}
